﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DAP.Adorners;

namespace VietImageSoftware
{
	/// <summary>
	/// Interaction logic for ResizeImageForm.xaml
	/// </summary>
	public partial class ResizeImageForm : Window
	{
        VietImage.Models.Image currentImage;

        public VietImage.Models.Image CurrentImage
        {
            get { return currentImage; }
            set { currentImage = value; }
        }

        private Image croppedImage;

        public Image CroppedImage
        {
            get { return croppedImage; }
            set { croppedImage = value; }
        }

        CroppingAdorner _clp;
        FrameworkElement _felCur = null;

		public ResizeImageForm()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}

        private void RemoveCropFromCur()
        {
            AdornerLayer aly = AdornerLayer.GetAdornerLayer(_felCur);
            aly.Remove(_clp);
        }

        private void AddCropToElement(FrameworkElement fel)
        {
            if (_felCur != null)
            {
                RemoveCropFromCur();
            }
            Rect rcInterior = new Rect(
                fel.ActualWidth * 0.2,
                fel.ActualHeight * 0.2,
                fel.ActualWidth * 0.6,
                fel.ActualHeight * 0.6);
            AdornerLayer aly = AdornerLayer.GetAdornerLayer(fel);
            _clp = new CroppingAdorner(fel, rcInterior);
            aly.Add(_clp);
            imgRIImgCrop.Source = _clp.BpsCrop();
            _clp.CropChanged += CropChanged;
            _felCur = fel;
            //if (rbRed.IsChecked != true)
            //{
            //    SetClipColorGrey();
            //}
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            using (var stream = new System.IO.MemoryStream(currentImage.Source.ToArray()))
            {

                //System.Drawing.Bitmap bm = new System.Drawing.Bitmap(stream);
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.StreamSource = stream;
                bitmap.StreamSource.Position = 0;
                bitmap.EndInit();
                bitmap.Freeze();

                imgChurch.Source = bitmap;
            }
            AddCropToElement(imgChurch);
            Color clr = Colors.Black;
            clr.A = 110;
            _clp.Fill = new SolidColorBrush(clr);
            RefreshCropImage();
        }

        private void RefreshCropImage()
        {
            if (_clp != null)
            {
                Rect rc = _clp.ClippingRectangle;

                tblkClippingRectangle.Text = string.Format(
                    "Clipping Rectangle: ({0:N1}, {1:N1}, {2:N1}, {3:N1})",
                    rc.Left,
                    rc.Top,
                    rc.Right,
                    rc.Bottom);
                imgRIImgCrop.Source = _clp.BpsCrop();
            }
        }

        private void CropChanged(Object sender, RoutedEventArgs rea)
        {
            RefreshCropImage();
        }

        //private void CropControls_Checked(object sender, RoutedEventArgs e)
        //{
        //    if (dckControls != null)
        //    {
        //        dckControls.Visibility = Visibility.Visible;
        //        AddCropToElement(dckControls);
        //        RefreshCropImage();
        //    }
        //}

        //private void CropImage_Checked(object sender, RoutedEventArgs e)
        //{
        //    if (dckControls != null && imgChurch != null)
        //    {
        //        dckControls.Visibility = Visibility.Hidden;
        //        AddCropToElement(imgChurch);
        //        RefreshCropImage();
        //    }
        //}

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            RefreshCropImage();
        }

        private void btnRIOK_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            croppedImage = imgRIImgCrop;           
            this.Close();
        }

        private void btnRICancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
			this.Close();
        }

        private void gridResizeImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            try
            {
                this.DragMove();
            }
            catch (Exception ex)
            {
            }
        }

        //private void SetClipColorRed()
        //{
        //    if (_clp != null)
        //    {
        //        _clp.Fill = _brOriginal;
        //    }
        //}

        //private void SetClipColorGrey()
        //{
        //    if (_clp != null)
        //    {
        //        Color clr = Colors.Black;
        //        clr.A = 110;
        //        _clp.Fill = new SolidColorBrush(clr);
        //    }
        //}

        //private void Red_Checked(object sender, RoutedEventArgs e)
        //{
        //    SetClipColorRed();
        //}

        //private void Grey_Checked(object sender, RoutedEventArgs e)
        //{
        //    SetClipColorGrey();
        //}
	}
}